package com.harmontronics.hanma.paas.paas.config;

import static com.google.common.collect.Lists.newArrayList;

import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author Chasen
 */
@Configuration
public class Swagger2 {

  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .globalOperationParameters(Lists.newArrayList(
            new ParameterBuilder().name("Access-Token")
                .description("token 调用时使用")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build(),
                new ParameterBuilder().name("Domain-Suffix")
                        .description("Domain-Suffix")
                        .modelRef(new ModelRef("string"))
                        .parameterType("header")
                        .required(false)
                        .build(),
                new ParameterBuilder().name("App-Token")
                        .description("App-Token")
                        .modelRef(new ModelRef("string"))
                        .parameterType("header")
                        .required(false)
                        .build(),
                new ParameterBuilder().name("App-Preview")
                        .description("App-Preview")
                        .modelRef(new ModelRef("string"))
                        .parameterType("header")
                        .required(false)
                        .build(),
    new ParameterBuilder().name("App-Unique")
            .description("App-Unique")
            .modelRef(new ModelRef("string"))
            .parameterType("header")
            .required(false)
            .build()
        ))
        .apiInfo(apiInfo())
        .select()
        // 加了ApiOperation注解的类，才生成接口文档
        .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .paths(PathSelectors.any())
        .build()
        .securitySchemes(security());
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("DDD-Generato平台")
        .description("DDD-Generator文档")
        .termsOfServiceUrl("http://www.mini.io")
        .version("1.0.0")
        .build();
  }

  private List<ApiKey> security() {
    return newArrayList(new ApiKey("Access-Token", "Access-Token", "header"));
  }
}
